'use strict';

import { platform_joninnewsModel } from "../model/platform_joinnews";
import { ServiceBase, Exceptions } from '../kalend';
import config from "../middleware/config";
var moment = require('moment');

class Platformjoinnews_Service extends ServiceBase {
    constructor() {
        super(platform_joninnewsModel)
    }

    async querybypage(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        let result = await super.queryPagination(offset, limit, {
            //filter: { status: 1 },
            select: 'newstitle newsthumbnail newspic newscontent merchantName merchantlabel createTime merchantid'
        });

        result.rows = result.rows.map((item) => {
          item.createTime=moment(item.createTime).format('YYYY-MM-DD');
          return item;
        });
        return result;
    }

    async save(doc: any) {
        const apiurl = config.get("apiforarea")["imgurl"];
        doc.newspic = apiurl + doc.newspic;
        doc.newsthumbnail = apiurl + doc.newsthumbnail;
        if (doc._id) {
            return this.update(doc._id, doc);
        } else {
            return this.create(doc);
        }
    }

    async lock(userid: any) {
        return this.update(userid, { status: 0 });
    }
    async open(userid: any) {
        return this.update(userid, { status: 1 });
    }
}

export const platformjoinnews_Service = new Platformjoinnews_Service();